//SocketLibSocket.h
 

#ifndef SOCKETLBSOCKET_H_
#define SOCKETLBSOCKET_H_

#include <string>
#include <set>
#include "SocketLibTypes.h"
#include "SocketLibErrors.h"


namespace SocketLib{
	
	class Socket{
	protected:
		int m_sock;
		struct sockaddr_in m_localinfo;
		bool m_isblocking;
	public:
		Socket(int);
		~Socket();
		int CreateSock();
		int GetSock();
		port GetLocalPort();
		ipaddress GetLocalAddress();
		void Close();
		void SetBlocking(bool);
	};

	class DataSocket:public Socket{
	protected:
		bool m_connected;
		struct sockaddr_in m_remoteinfo;
	public:
		DataSocket(int sock=-1);
		~DataSocket();
		ipaddress GetRemoteAddress();
		port  GetRemotePort();
		bool IsConnected();
		void Connect(const char* ipaddress, int port);
		int  Send( const char* p_buffer, int p_size );
		int  Receive(char* buff, int len);
		void Close();
	};

	class ListeningSocket:public Socket{
	protected:
		bool m_listening;
	public:
		ListeningSocket();
		~ListeningSocket();
		void Listen(port p_port, ipaddress ip = htonl(INADDR_ANY), int backlog = 1);
		DataSocket Accept();
		bool IsListening();
		void Close();
	};


	class SocketSet{
	protected:
		fd_set m_set;
		fd_set m_activityset;
		std::set<int> m_socketdescs;
	public:
		SocketSet();
		~SocketSet();
		void AddSocket(Socket&);
		void RemoveSocket(Socket&);
		int  Poll(long = 0);
		bool HasActivity(Socket&);
	};



} 

#endif